var codeAssemblyFun = {};
codeAssemblyFun.resMap;
codeAssemblyFun.initSuccess = true;
//空值处理
codeAssemblyFun.nvl = function (data) {
    if (data) {
        return data;
    }
    return "";
}
//初始化
codeAssemblyFun.init = function () {
    //打开loading框
    $('.loading').css('display','block')
    var selectTableNow = $("#selectTableNow").val();
    var selectId = $("#selectId").val();
    $.ajax({
        type: "GET",
        url: "/code/columnInfo?selectTableNow=" + selectTableNow + "&selectId=" + selectId + "&n_" + (new Date().getTime()),
        success: function (data) {
            if (data.code == '000000') {
                codeAssemblyFun.resMap = data;
                codeAssemblyFun.initPage();
                codeAssemblyFun.initSort();
            } else {
                codeAssemblyFun.initSuccess = false;
                alert(data.message)
            }
        }
    });
}
codeAssemblyFun.initSort = function () {
     Sortable.create(document.getElementById('ctrlTableXX'), {
        animation: 150,
        store: {//缓存到localStorage
            get: function (sortable) {

            },
            set: function (sortable) {

            }
        }
    });
}


//初始化页面
codeAssemblyFun.initPage = function () {
    codeAssemblyFun.initCtrlPage();
    //初始化目录
    var selectTableNowValue = $("#selectTableNowValue").val().toLowerCase();
    selectTableNowValue = selectTableNowValue.replaceAll("_", ".");
    selectTableNowValue = selectTableNowValue.replaceAll("-", ".");
    $("#selectTableBaseDir").val("org.springblade.modular." + selectTableNowValue);
    //关闭loading框
    $('.loading').css('display','none')


}
//查询区域选择
codeAssemblyFun.initCtrlPage = function () {
    $("#defDiv").html("");
    var h = "<table style='width:100%;' border='1px'>";
    h += "<thead>";
    h += "<tr>";
    h += "<td style='width:220px;' align='center'>名称</td>";
    h += "<td style='width:120px;' align='center'>类型</td>";
    // h += "<td style='width:120px;' align='center'><span style='color:#F00;font-weight: bold'>查询显示</span></td>";
    // h += "<td style='width:120px;' align='center'><span style='color:#F00;font-weight: bold'>卡片显示</span></td>";
    // h += "<td style='width:120px;' align='center'><span style='color:#F00;font-weight: bold'>列表显示</span></td>";
    h += "<td style='width:120px;' align='center'>模糊查询</td>";
    h += "<td style='width:120px;' align='center'>批量查询</td>";
    h += "<td style='width:120px;' align='center'>单独查询</td>";
    h += "<td style='width:120px;' align='center'>显示类型</td>";
    h += "<td style='width:120px;' align='center'>字典编码</td>";
    h += "<td align='center'></td>";
    h += "</tr>";
    h += "</thead>";
    h += "<tbody  id='ctrlTableXX'>";
    var attList = codeAssemblyFun.resMap.attList;
    var item;
    var stringDisabled;
    var numberDisabled;
    for (var index = 0; index < attList.length; index++) {
        item = attList[index];

        if (item.type == 'String') {
            stringDisabled = " disabled "
            numberDisabled = "";
        } else {
            stringDisabled = ""
            numberDisabled = " disabled ";
        }


        h += "<tr>";
        h += "<td>";
        if(item .camelName == item .label){
            h += item.label;
        } else {
            h += "<span title='"+item.label+"'>"+ item .camelName + "</span>";
        }
        h += "</td>";
        h += "<td>";
        h += item.type;
        h += "</td>";
        // h += "<td>";
        // h += "<select id='ctrl_display_query_" + item.camelName + "' name='ctrl_display_query_" + item.camelName + "'  style='width:100%;'>";
        // h += "<option value='N'> </option>";
        // h += "<option value='Y'>是</option>";
        // h += "</select>";
        // h += "</td>";
        // h += "<td>";
        // h += "<select id='ctrl_display_form_" + item.camelName + "' name='ctrl_display_form_" + item.camelName + "'  style='width:100%;'>";
        // h += "<option value='N'> </option>";
        // h += "<option value='Y'>是</option>";
        // h += "</select>";
        // h += "</td>";
        // h += "<td>";
        // h += "<select id='ctrl_display_list_" + item.camelName + "' name='ctrl_display_list_" + item.camelName + "'  style='width:100%;'>";
        // h += "<option value='N'> </option>";
        // h += "<option value='Y'>是</option>";
        // h += "</select>";
        // h += "</td>";
        h += "<td>";
        h += "<select id='ctrl_like_search_" + item.camelName + "'  name='ctrl_like_search_" + item.camelName + "'  style='width:100%;'" + numberDisabled + ">";
        if (item.type == 'String') {
            h += "<option value='N'> </option>";
            h += "<option value='Y'>是</option>";
        } else {
            h += "<option value=''> </option>";
        }
        h += "</select>";
        h += "</td>";
        h += "<td>";
        h += "<select id='ctrl_batch_search_" + item.camelName + "' name='ctrl_batch_search_" + item.camelName + "'  style='width:100%;'>";
        if (item.camelName == 'id') {
            h += "<option value='Y'>是</option>";
            h += "<option value='N'> </option>";
        } else {
            h += "<option value='N'> </option>";
            h += "<option value='Y'>是</option>";
        }
        h += "</select>";
        h += "</td>";
        h += "<td>";
        h += "<select id='ctrl_only_search_" + item.camelName + "' name='ctrl_only_search_" + item.camelName + "'  style='width:100%;'>";
        if (item.camelName == 'pid'
            || item.camelName == 'pId'
            || item.camelName == 'headId'
            || item.camelName == 'srcId') {
            h += "<option value='Y'>是</option>";
            h += "<option value='N'> </option>";
        } else if (item.camelName == 'id' || item.camelName == 'ID') {
            h += "<option value='N'> </option>";
        } else {
            h += "<option value='N'> </option>";
            h += "<option value='Y'>是</option>";
        }
        h += "</select>";
        h += "</td>";
        h += "<td>";
        h += "<select id='ctrl_show_type_" + item.camelName + "'  name='ctrl_show_type_" + item.camelName + "'  style='width:100%;'" + numberDisabled + ">";
        h += "<option value=''></option>";
        if (item.type == 'String') {
            h += "<option value='Dic'>字典</option>";
            // h += "<option value='DeptSelect'>部门选择</option>";
            // h += "<option value='UserSelect'>人员选择</option>";
        }
        h += "</select>";
        h += "</td>";
        h += "</td>";
        h += "<td>";
        h += "<input type='text' id='ctrl_dict_code_" + item.camelName + "'  name='ctrl_dict_code_" + item.camelName + "' " + numberDisabled + ">";
        h += "</td>";
        h += "<td>";
        h += "<input type='hidden' id='ctrl_code_" + item.camelName + "'  name='ctrl_code_" + item.camelName + "' value='" + item.camelName + "'>";
        h += "<input type='hidden' id='ctrl_type_" + item.camelName + "'  name='ctrl_type_" + item.camelName + "' value='" + item.type + "'>";
        h += "<input type='hidden' id='ctrl_label_" + item.camelName + "'  name='ctrl_label_" + item.camelName + "' value='" + item.label + "'>";
        var sort = index;
        if (item.sort) {
            sort = item.sort;
        }
        h += "<input type='hidden' id='ctrl_sort_" + item.camelName + "'  name='ctrl_sort_" + item.camelName + "' value='" + sort + "'>";
        h += "</td>";
        h += "</tr>";
    }
    h += "</tbody>";
    h += "</table>";
    $("#defDiv").html(h);
}

codeAssemblyFun.create = function () {
    codeAssemblyFun.downloadCode("N");
};
codeAssemblyFun.downloadCode = function (downMode) {
    var ctrlData = [];
    var sortNow = 0;
    //打开loading框
    $('.loading').css('display','block')
    $("[name^='ctrl_code_']").each(function (itemIndex, itemObj) {

        var camelName = $(itemObj).val();
        var displayQueryValue = $("#ctrl_display_query_" + camelName).val();
        var displayFormValue = $("#ctrl_display_form_" + camelName).val();
        var displayListValue = $("#ctrl_display_list_" + camelName).val();
        var likeQueryValue = $("#ctrl_like_search_" + camelName).val();
        var batchQueryValue = $("#ctrl_batch_search_" + camelName).val();
        var onlyQueryValue = $("#ctrl_only_search_" + camelName).val();

        var showTypeValue = $("#ctrl_show_type_" + camelName).val();
        var dictCodeValue = $("#ctrl_dict_code_" + camelName).val();

        var typeValue = $("#ctrl_type_" + camelName).val();
        var labelValue = $("#ctrl_label_" + camelName).val();
        var itemData = {
            "name": codeAssemblyFun.nvl(camelName),
            "queryShow": codeAssemblyFun.nvl(displayQueryValue),
            "formShow": codeAssemblyFun.nvl(displayFormValue),
            "listShow": codeAssemblyFun.nvl(displayListValue),
            "likeQuery": codeAssemblyFun.nvl(likeQueryValue),
            "batchQuery": codeAssemblyFun.nvl(batchQueryValue),
            "onlyQuery": codeAssemblyFun.nvl(onlyQueryValue),
            "showType": codeAssemblyFun.nvl(showTypeValue),
            "dictCode": codeAssemblyFun.nvl(dictCodeValue),
            "type": codeAssemblyFun.nvl(typeValue),
            "label": codeAssemblyFun.nvl(labelValue),
            "sort": sortNow++
        };
        ctrlData[ctrlData.length] = itemData;
    })

    var selectTableNow = $("#selectTableNow").val();
    var selectMode = $("#selectMode").val();
    var selectProjectName = $("#selectProjectName").val();
    var selectTableBaseDir = $("#selectTableBaseDir").val();
    var selectId = $("#selectId").val();
    if (selectProjectName == null) {
        layer.alert("请输入项目名称");
        return;
    }
    if (selectTableBaseDir == null) {
        layer.alert("请输入项目存储地址");
        return;
    }
    $.ajax({
        type: "POST",
        data: {
            "selectId": selectId,
            "selectTableNow": selectTableNow,
            "selectProjectName": selectProjectName,
            "selectTableBaseDir": selectTableBaseDir,
            "selectMode": selectMode,
            "ctrlData": JSON.stringify(ctrlData),
        },
        url: "/code/build?n_=" + (new Date().getTime()),
        success: function (data) {
            //关闭loading框
            $('.loading').css('display','none')
            if('N' != downMode){
                window.location.href = "/code/down";
            }
        }
    });


};


$(function () {
    codeAssemblyFun.init();
});